<?php
// Hello and welcome to the Skeleton module. Don't worry it's not as scary as it
// sounds. I'm assuming you are reading this comment because you are either bored
// or wish to make a module. If it's the latter then "Yay you!" :)
//
// You'll notice that this module is HEAVILY over-commented. The idea is that you
// may use the skelton module as a base module to get things up and running quickly
// and/or to teach you a thing or two. I personally advise however that when writing
// your module you take the time to remove comments as you see fit (providing you
// understand the code of course), to make things a little neater and less hectic
// for other potential eyes that may glance upon your magnificent masterpiece!
//
// Good luck!

#### SKELETON MODEL ############################################################
class SkeletonModel {

	#### CONSTRUCTOR ###########################################################
	function SkeletonModel() {
		Base::getThis();

		// Default table to perform actions on
		$this->default_table = $this->db->table['skeleton'];
	}

	#### GET ITEMS #############################################################
	function getItems($limit = FALSE) {

		// Build our SQL depending on supplied arguments
		$this->db->select($this->default_table);
		$this->db->limit($limit);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);

		// Put our result rows into an array ready to be returned to a foreach
		return $this->db->result();
	}

	#### GET ENABLED ITEMS #####################################################
	function getEnabledItems($limit = FALSE) {

		// Build our SQL depending on supplied arguments
		$this->db->select($this->default_table);
		$this->db->where('disabled = 0');
		$this->db->limit($limit);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);

		// Put our result rows into an array ready to be returned to a foreach
		return $this->db->result();
	}

	#### GET ITEM BY ID ########################################################
	function getItemByID($id) {

		// Our query SQL
		$this->db->select($this->default_table).
		$this->db->where('id = '.$id);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		return $this->db->fetchArray();
	}

	#### GET ITEM BY SEF #######################################################
	function getItemBySef($sef) {

		// Our query SQL
		$this->db->select($this->default_table).
		$this->db->where("sef_title = '$sef'");

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		return $this->db->fetchArray();
	}

	#### ITEM COUNT ############################################################
	function getItemCount() {

		// Build our SQL query
		$this->db->count($this->default_table);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		list($count) = $this->db->fetchRow();

		return $count;
	}

	#### INSERT ITEM ###########################################################
	function insertItem() {

		// Make sef_title
		$this->post['sef_title'] = sef($this->post['title']);

		// Build our SQL query
		$this->db->insert($this->default_table, $this->post);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
	}

	#### UPDATE ITEM ###########################################################
	function updateItem($id) {

		// Make sef_title
		$this->post['sef_title'] = sef($this->post['title']);

		// Build our SQL query
		$this->db->update($this->default_table, $this->post);
		$this->db->where('id='.$id);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
	}

	#### DELETE ITEM ###########################################################
	function deleteItem($id) {

		// Build our SQL query
		$this->db->delete($this->default_table);
		$this->db->where('id = '.$id);
		$this->db->limit(1);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
	}

	#### UPDATE ITEM STATUS ####################################################
	function updateItemStatus($id) {

		$item = $this->getItemByID($id);

		$fields['disabled'] = ($item['disabled'] ? 0 : 1);

		// Build our SQL query
		$this->db->update($this->default_table, $fields);
		$this->db->where('id='.$id);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
	}
}
?>